package com.example.demo2.student;

import com.example.demo2.Util.DBHelper;
import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import java.io.IOException;
import java.rmi.server.UID;
import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;
@WebServlet(name = "AddLoginIDServlet", value = "/AddLoginIDServlet")
public class AddLoginIDServlet extends HttpServlet {
    private static final long serialVersionUID = 1L;

    protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        request.setCharacterEncoding("utf-8");
        String phone = request.getParameter("UID");
        String pwd = request.getParameter("upwd");
        boolean result = false;
        PreparedStatement preparedStatement = null;
        Connection conn = null;
        ResultSet rs = null;
        com.example.demo2.Util.DBHelper DBHelper = new DBHelper();
        conn = DBHelper.getConn();
        int i;
        try {
            preparedStatement = conn.prepareStatement("insert into webdesign.user(password, phone) values (?,?)");
            preparedStatement.setString(1, pwd);
            preparedStatement.setString(2, phone);
            i = preparedStatement.executeUpdate();
            DBHelper.closeAll(conn, preparedStatement, rs);
        } catch (SQLException e) {
            throw new RuntimeException(e);
        }
        response.setContentType("text/html;charest=UTF-8");
        response.setCharacterEncoding("utf-8");
        if (i == 0) {
            request.setAttribute("error4", "addError");
            request.getRequestDispatcher("register.jsp").forward(request, response);
        } else {
            request.setAttribute("error4", "noaddError");
            request.getRequestDispatcher("login.jsp").forward(request, response);
        }

    }

    protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        doGet(request, response);
    }
}
